package com.lams.action.system;

import java.util.*;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.lams.action.SubAction;
import com.lams.service.system.IRoleFacade;
import com.lams.service.system.IUserFacade;
import com.lams.util.Encrypt_MD5;
import com.lams.util.ResultEntity;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class UserAction extends SubAction {
	// 针对于页面的成员变量
	private IUserFacade uf;
	// private SUser u;
	private String ROLE_ID;
	private String USER_NO;
	private String PASSWORD;

	// private SUser u = new SUser();
	public String getROLE_ID() {
		return ROLE_ID;
	}

	public void setROLE_ID(String rOLE_ID) {
		ROLE_ID = rOLE_ID;
	}

	// 构造方法，用于对成员变量赋初值
	public UserAction() {
	}

	public IUserFacade getUf() {
		return uf;
	}

	public void setUf(IUserFacade uf) {
		this.uf = uf;
	}

	private String checkCode;

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode.toLowerCase();
	}

	// 处理login请求方法
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String login() {
		ResultEntity result = new ResultEntity();
		Map user = new HashMap();
		user.put("USER_NO", USER_NO);
		user.put("PASSWORD", PASSWORD);
		if (uf.login(user).equals("success")) {
			result.setSuccess(true);
			result.setMessage("登陆成功");
		} else {
			result.setSuccess(false);
			result.setMessage("帐号或密码错误");
		}
		System.out.println(ResultEntity.decode(result));
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}

	public String getUserListByRole() {
		ResultEntity result = uf.operateUser(this.getROLE_ID());
		this.setResult(ResultEntity.decode(result));
		return SUCCESS;
	}

	// 处理exit请求方法
	public String exit() {
		HttpSession hs = ServletActionContext.getRequest().getSession();
		hs.removeAttribute("role");
		hs.removeAttribute("user");
		return SUCCESS;
	}

	public String getUSER_NO() {
		return USER_NO;
	}

	public void setUSER_NO(String uSER_NO) {
		USER_NO = uSER_NO;
	}

	public String getPASSWORD() {
		return PASSWORD;
	}

	public void setPASSWORD(String pASSWORD) {
		PASSWORD = Encrypt_MD5.encrypt(pASSWORD);
	}

}
